package com.koron.css2.projectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.projectCenter.bean.query.PjConsultQueryBean;
import com.koron.css2.projectCenter.mapper.PjConsultMapper;
import com.koron.css2.projectCenter.vo.PjConsultListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 用水咨询列表
 * @author 许海宜
 * 2020年3月5日
 */
public class PjConsultList implements ServerInterface {

	@Override
	@SuppressWarnings("rawtypes")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换为QueryBean
			PjConsultQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PjConsultQueryBean.class);
			if(StringUtils.isBlank(queryBean.getBeginDate())||StringUtils.isBlank(queryBean.getEndDate())) {
				return MessageBean.create(Constant.NOT_NULL, "咨询日期不能为空", null);
			}
			//获取Mapper实例
			PjConsultMapper mapper = factory.getMapper(PjConsultMapper.class);			
			//设置分页参数
			PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
			//执行SQL
			List<PjConsultListVO> list = mapper.list(queryBean);
			//获取紧急程度数据字典
			Map<String, String> egd = Tools.mapDicByCode(factory, "EGD");
			//获取来源渠道数据字典
			Map<String, String> PIS = Tools.mapDicByCode(factory, "PIS");
			for(PjConsultListVO bean:list) {
				//转换数据字典
				bean.setUrgentFlagName(egd.get(bean.getUrgentFlag()));
				bean.setSourceFlag(PIS.get(bean.getSourceFlag()));
				//转换状态
				if("suspend".equals(bean.getProcessState())) {
					bean.setProcessState("已暂停");
				}else {
					bean.setProcessState(Tools.transProcessStatus(bean.getProcessState()));
				}
				
			}
			//输出分页结果
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			info.setData(new PageInfo<>(list));
			return info;
		} catch (Exception e) {
			logger.error("用水咨询列表查询失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}


}
